/**
 * 
 */
package fr.utbm.gi.vi51.slizers.framework.agent;

import java.util.Collections;
import java.util.List;

import org.janusproject.kernel.address.AgentAddress;

import fr.utbm.gi.vi51.slizers.framework.environment.Perception;
import fr.utbm.gi.vi51.slizers.pw.agent.action.CreatureActionInfluence;

/**
 * Abstract implementation of an agent body.
 * 
 * @author Tarik Belabbas
 * 
 */
public abstract class AgentBody extends AbstractSituatedObject implements Body
{
	/**
	 * The address of the agent which owns the agent body.
	 */
	private final AgentAddress owner;
	
	/**
	 * The action influence of the agent body.
	 */
	private CreatureActionInfluence actionInfluence = null;
	
	/**
	 * The objects perceived by the agent body.
	 */
	private List< Perception > perceptions = Collections.emptyList();
	
	/**
	 * Parameterized constructor.
	 * 
	 * @param _owner the address of the owner
	 */
	public AgentBody( AgentAddress _owner )
	{
		this.owner = _owner;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.utbm.gi.vi51.slizers.framework.environment.Body#move(fr.utbm.gi.vi51
	 * .slizers.framework.environment.Influence)
	 */
	@Override
	public void influenceBody( CreatureActionInfluence _actionInfluence )
	{
		this.actionInfluence = _actionInfluence;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.utbm.gi.vi51.slizers.framework.environment.Body#perceive()
	 */
	@Override
	public List< Perception > getPerceivedObjects()
	{
		return Collections.unmodifiableList( this.perceptions );
	}
	
	/**
	 * Returns the owner of the agent body.
	 * 
	 * @return the owner of the agent body.
	 */
	public AgentAddress getOwner()
	{
		return this.owner;
	}

	/**
	 * Consume and returns the influence.
	 * 
	 * @return the consumed influence
	 */
	public CreatureActionInfluence consumeInfluence()
	{
		CreatureActionInfluence influence = this.actionInfluence;
		this.actionInfluence = null;
		
		if ( influence != null )
		{
			influence.setEmitter( getOwner() );
		}
		
		return influence;
	}

	/**
	 * Set the perceptions.
	 * 
	 * @param perceptions
	 */
	public void setPerceptions( List< Perception > _perceptions )
	{
		assert ( _perceptions != null );
		
		this.perceptions = _perceptions;
	}
}
